package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.dao.BaseDao;
import com.atguigu.dao.DictDao;
import com.atguigu.dao.HouseDao;
import com.atguigu.entity.House;
import com.atguigu.service.HouseService;
import com.atguigu.vo.HouseQueryVo;
import com.atguigu.vo.HouseVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;

@Service(interfaceClass = HouseService.class)
@Transactional
public class HouseServiceImpl extends BaseServiceImpl<House> implements HouseService {
    @Autowired
    private HouseDao houseDao;
    @Autowired
    private DictDao dictDao;
    @Override
    protected BaseDao<House> getEntityDao() {
        return houseDao;
    }

    @Override
    public void publish(Long houseId, Integer status) {
        House house = new House();
        house.setId(houseId);
        house.setStatus(status);
        houseDao.update(house);
    }

    @Override
    public PageInfo<HouseVo> findPageList(Integer pageNum, Integer pageSize, HouseQueryVo houseQueryVo) {
//        开启分页
        PageHelper.startPage(pageNum,pageSize);
//        调用HouseDao中前端分页及带条件查询的方法
        Page<HouseVo> page=houseDao.findPageList(houseQueryVo);
        for (HouseVo houseVo : page) {
            String houseTypeName = dictDao.getNameById(houseVo.getHouseTypeId());
            String floorName = dictDao.getNameById(houseVo.getFloorId());
            String directionName = dictDao.getNameById(houseVo.getDirectionId());
            houseVo.setHouseTypeName(houseTypeName);
            houseVo.setFloorName(floorName);
            houseVo.setDirectionName(directionName);
        }
        return new PageInfo<>(page,5);

    }


    //  重写目的展示房源中户型，楼层，朝向
    @Override
    public House getById(Serializable id) {
        House house = houseDao.getById(id);
        String houseType = dictDao.getNameById(house.getHouseTypeId());
        String floor = dictDao.getNameById(house.getFloorId());
        String buildStructure = dictDao.getNameById(house.getBuildStructureId());
        String decoration = dictDao.getNameById(house.getDecorationId());
        String direction = dictDao.getNameById(house.getDirectionId());
        String houseUse = dictDao.getNameById(house.getHouseUseId());
        house.setHouseTypeName(houseType);
        house.setFloorName(floor);
        house.setBuildStructureName(buildStructure);
        house.setDecorationName(decoration);
        house.setDirectionName(direction);
        house.setHouseUseName(houseUse);
        return house;
    }
}
